package com.ssy.lingxi.order.repository;

import com.ssy.lingxi.order.entity.OrderDO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 订单Jpa仓库
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-15
 */
@Repository
public interface OrderRepository extends JpaRepository<OrderDO, Long>, JpaSpecificationExecutor<OrderDO>, QuerydslPredicateExecutor<OrderDO> {
    List<OrderDO> findByOrderNoIn(List<String> orderNos);

    OrderDO findFirstByOrderNo(String orderNo);

    int countByIdInAndBuyerMemberIdAndBuyerRoleIdAndVendorMemberIdAndVendorRoleId(List<Long> orderIds, Long buyerMemberId, Long buyerRoleId, Long vendorMemberId, Long vendorRoleId);

    /**
     * 根据订单id查看退款金额
     *
     * @param orderIds 订单Id
     * @return 返回相加在一起的全部退款金额
     */
    @Query(value = "select sum(op.ref_return_amount) FROM ord_order oo , ord_product op WHERE oo.id =op.order_id and oo.id in ?1", nativeQuery = true)
    BigDecimal findByOrderRefundAmount(List<Long> orderIds);

}
